<template>
  <el-dialog v-model="visible" :title="title" width="520" show-footer show-zoom resize :close-on-click-modal="false"
    :close-on-press-escape="false" @close="close">
    <template #default>
      <el-form ref="formRef" :rules="rules" :model="form" label-align="right" label-width="120" size="default"
        label-position="top">

        <el-form-item label="楼层名称" prop="floor_name">
          <el-input v-model="form.floor_name" placeholder="请输入楼层名称"></el-input>
        </el-form-item>
        <el-form-item label="所属仓库" prop="warehouse_id">
          <SelectWarehouse class="w-full" v-model="form.warehouse_id" placeholder="请选择" @change="changeWarehouse" :disabled="handleType == 'edit'">
          </SelectWarehouse>
        </el-form-item>

        <el-form-item label="是否启用" prop="status">
          <el-select class="w-full" v-model="form.status" placeholder="请选择">
            <el-option v-for="item in props.options['base.floor']?.[0].floorStatus" :key="item.value" :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="flex justify-center py-2">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="save(formRef)">保 存</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import { getFloorDetail, saveFloor, updateFloor } from '../api/index'
import { reactive, ref, toRefs } from 'vue'
import { ElMessage } from 'element-plus'
import SelectWarehouse from '@/components/SelectWarehouse'

const visible = ref(false)
const loading = ref(false)
const formRef = ref(null)
const emits = defineEmits(['query'])

const props = defineProps({
  options: {
    type: Object,
    default: {}
  }
})

const data = reactive({
  form: {},
  title: '新增',
  handleType: null,
  rules: {
    floor_name: [{
      required: true, message: '请输入楼层名称', trigger: 'blur'
    }],
    warehouse_id: [{
      required: true, message: '请选择所属仓库', trigger: 'change'
    }],
    status: [{
      required: true, message: '请选择是否启用', trigger: 'change'
    }],
  },
  wareDataList: []
})
const { form, title, handleType, rules, wareDataList } = toRefs(data)

const statusMap = new Map([
  ['add', '新增'],
  ['edit', '编辑'],
  ['view', '查看'],
])

const open = (type, id) => {
  title.value = statusMap.get(type)
  handleType.value = type
  visible.value = true
  if (id) getDetail(id)
}

const initForm = () => {
  form.value = getForm()
}
// 初始化
const getForm = () => {
  return {
    status: 1
  }
}

onMounted(() => {
  initForm()
})

const getDetail = async (id) => {
  try {
    const res = await getFloorDetail({
      id
    })
    form.value = res.data
  } catch (e) {
    throw Error(e)
  }
}

const changeWarehouse = async (val) => {
  console.log(val)
  form.value.warehouse_name = val
}

const save = () => {
  if (!formRef.value) return
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      if (form.value.id) {
        updateFloor(form.value)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      } else {
        saveFloor(form.value)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      }
    }
  })
}
const close = () => {
  initForm()
  visible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.borderNone {
  :deep(.el-input__wrapper) {
    border: none;
    box-shadow: none
  }
}
</style>
